package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaModelDto;
import com.ysd.lis.dto.sta.StaModelParam;
import com.ysd.lis.entity.sta.StaModel;
import com.ysd.lis.mapper.sta.StaModelMapper;
import com.ysd.lis.service.sta.StaModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_Model 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaModelServiceImpl extends ServiceImpl<StaModelMapper, StaModel> implements StaModelService {
    @Autowired
    StaModelMapper staModelMapper;

    @Override
    public Result findStaModelList(StaModelParam pageParam) {
        MPJLambdaWrapper<StaModel> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaModel::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaModel::getModelCode, pageParam.getSeachValue())
                    .or().like(StaModel::getModelName,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaModel::getSeq);
        List<StaModel> supplierPage = staModelMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addStaModel(StaModel staModel) {
        int insert = staModelMapper.insert(staModel);
        if(insert>0){
            return Result.succ(1,"添加成功",staModel);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editStaModel(StaModel supplier) {
        int updateById = staModelMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteStaModelList(StaModelDto dto) {
        List<StaModel> supplierList = dto.getStaModelList();
        List<String> ids = supplierList.stream().map(StaModel::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
